package com.wn55.hrms.web.controller;



import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Trainer;

import com.wn55.hrms.service.TrainerService;

import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author z
 */
@RestController
@RequestMapping("trainer")
@Api(value = "此接口用于操作培训讲师功能")
public class TrainerController {

    @Autowired
    TrainerService trainerService;


    @GetMapping("/trainerList")
    @ApiOperation(value = "分页查询讲师列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "int",name="now",value = "查询当前页",required = false,defaultValue = "1"),
            @ApiImplicitParam(paramType = "query",dataType = "int",name="size",value ="查询每页条数",required = false,defaultValue = "2"),
            @ApiImplicitParam(paramType = "query",dataType = "string",name="name",value = "讲师姓名",required = false),

    })
    public ResultResponse<PageInfo<Trainer>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now ,
            @RequestParam(value = "size",defaultValue = "2")Integer size,
            String name){
        ResultResponse<PageInfo<Trainer>> result = null;
//        System.out.println("asssss:"+now+"页"+size+"条");

        try {
            PageInfo<Trainer> pageInfo = trainerService.trainerList(now,size,name);
            result = new ResultResponse<>(200,"查询讲师成功!",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"查询讲师失败!");
        }
        return result ;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改讲师")

    public ResultResponse<Void> modify(@RequestBody Trainer trainer){
        ResultResponse<Void> result = null;

        try {
            System.out.println(trainer.toString());
            trainerService.modify(trainer);
            result = new ResultResponse<>(200,"修改讲师成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"修改讲师失败!");
        }
        return result ;
    }



    @PostMapping("/add")
    @ApiOperation(value = "添加讲师")

    public ResultResponse<Void> add(@RequestBody Trainer trainer){
        ResultResponse<Void> result = null;


        try {
            trainer.setIsDelete(0);
           
            trainerService.add(trainer);
            result = new ResultResponse<>(200,"添加讲师成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"添加讲师失败!");
        }
        return result ;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据id删除讲师")

    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;

        try {
            trainerService.removeBykey(id);
            result = new ResultResponse<>(200,"删除讲师成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"删除讲师失败!");
        }
        return result ;
    }



}
